#ifndef GAMECONTROL_H
#define GAMECONTROL_H

#include <QObject>
#include "Robot.h"
#include "UserPlayer.h"
#include "Cards.h"

struct BetRecord
{
    BetRecord(){reset();}
    void reset(){
        player = nullptr;
        bet = 0;
        times = 0;
    }
    Player* player;
    int bet;
    int times;//记录是第几个下注的玩家
};

class GameControl : public QObject
{
    Q_OBJECT
public:
    //游戏状态
    enum GameStatus{
        DispatchCard,//发牌状态
        CallingLord,//叫地主状态
        PlayingHand//出牌状态
    };
    //玩家状态
    enum PlayerStatus{
        ThinkingForCallLord,//思考叫地主状态
        ThinkingForPlayHand,//思考出牌状态
        Winning//玩家胜利状态
    };

    explicit GameControl(QObject *parent = nullptr);

    //玩家相关属性函数设置
    void playerInit();//玩家初始化
    //获取玩家的实例对象函数、设置和获取当前玩家的函数
    Robot* leftRobot();
    Robot* rightRobot();
    UserPlayer* userPlayer();
    void setCurrentPlayer(Player* player);
    Player* currentPlayer();
    //获取出牌玩家和打出的牌
    Player* pendPlayer();
    Cards pendCards();

    //扑克牌相关属性函数设置
    void allCardsInit();//初始化扑克牌
    Card takeOneCard();//发一张牌函数
    Cards surPlusCards();//得到最后的三张底牌函数
    void resetCardData();//重置卡牌数据

    //准备叫地主、成为地主
    void startLordCard();//游戏正式开始
    void becomeLord(Player* player, int bet);
    //得到玩家下注的最高分数
    int playerMaxBet();

    //处理叫地主、处理出牌
    void onGrabBet(Player* player, int bet);
    void onPlayHand(Player* player, Cards& cards);
    //清空所有玩家的得分
    void clearPlayerScore();



signals:
    void playerStatusChange(Player* player, PlayerStatus status);
    //通知玩家抢地主了(flag记录是否是第一个叫地主的玩家)
    void notifyGradLordBet(Player* player, int bet, bool flag);
    //通知装窗口改变游戏状态
    void gameStatusChanged(GameStatus status);
    //通知玩家出牌了
    void notifyPlayHand(Player* player, Cards& card);
    //给玩家传递出牌的数据
    void pendingInfo(Player* player, Cards& card);

private:
    UserPlayer* m_user = nullptr;//玩家
    Robot* m_robotLeft = nullptr;//机器人A
    Robot* m_robotRight = nullptr;//机器人B
    Player* m_currPlayer = nullptr;//当前玩家
    Player* m_pendPlayer = nullptr;//当前玩家
    Cards m_pendCards;//暂存打出的牌
    Cards m_allCards;//保存所有牌的对象
    BetRecord m_betRecord;//保存玩家的下注信息
    int m_curBet = 0;//保存底分
};

#endif // GAMECONTROL_H
